package demo05

/**
 * @author xiaoZhao
 * @date 2022/6/30
 * @describe 单例模式
 */
object Test12_Singleton {
  def main(args: Array[String]): Unit = {
    val student = Student12.getInstance()
    student.printInfo()
    val student1 = Student12.getInstance()
    student1.printInfo()
    println(student == student1)

  }
}
// 构造器私有化
class Student12 private(val name: String, val age: Int) {
  def printInfo(): Unit = {
    println(s"Student11: $name $age ${Student11.school}")
  }
}

// 饿汉式
//object Student12 {
//  // 属性创建对象
//  private val student = new Student12("alice", 12)
//  // 通过方法返回对象实例
//  def getInstance(): Student12 = student
//
//}

// 懒汉式
object Student12{
  private var student: Student12 = _
  def getInstance(): Student12={
    if (student == null){
      student=new Student12("alice",12)
    }
    student
  }
}